<?php

if(!$_SESSION) session_start();

if(!isset($_SESSION['UserID']) || $_SESSION['UserID'] == "") header("Location:index.php");

// Connect to the database
////////////////////////////////////////////////////////

require_once("scripts/config.php");

// Load Functions
////////////////////////////////////////////////////////

require_once("scripts/functions.php");

// Insert Task
////////////////////////////////////////////////////////

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formVNT")) {

  $sql = sprintf("UPDATE tbltasks SET UserID = %s, ProjectID = %s, Subject = %s, date_due = %s, set_priority = %s, set_status = %s, set_type = %s, Body = %s, set_complete = %s WHERE TaskID = %s",
                        GetSQLValueString($_POST['UserID'], "int"),
                        GetSQLValueString($_POST['ProjectID'], "int"),
                        GetSQLValueString($_POST['Subject'], "text"),
                        GetSQLValueString(date("Y-m-d",strtotime($_POST['date_due'])), "date"),
                        GetSQLValueString($_POST['set_priority'], "int"),
                        GetSQLValueString($_POST['set_status'], "int"),
                        GetSQLValueString($_POST['set_type'], "int"),
                        GetSQLValueString($_POST['Body'], "text"),
                        GetSQLValueString($_POST['set_complete'], "int"),
                        GetSQLValueString($_POST['TaskID'], "int"));

  $task = sqlMod($sql, $db);

  if(strlen(trim($_POST['note_body']))>0){

      $sql = sprintf("INSERT INTO tbltask_notes SET Body = %s, TaskID = %s, UserID =%s",
                       GetSQLValueString($_POST['note_body'], "text"),
                       GetSQLValueString($_POST['TaskID'], "int"),
                       GetSQLValueString($_POST['UserID'], "int"));


      $task = sqlMod($sql, $db);
  }

  header("Location:task.php?up=1&ID=".$_POST['TaskID']);

}

if ((isset($_POST["add_note"])) && ($_POST["add_note"] == "add_note")) {

  $sql = sprintf("INSERT INTO tbltask_notes SET Body = %s, TaskID = %s, UserID =%s",
                       GetSQLValueString($_POST['note_body'], "text"),
                       GetSQLValueString($_POST['TaskID'], "int"),
                       GetSQLValueString($_POST['UserID'], "int"));

  $task = sqlMod($sql, $db);

}

// Load Data
////////////////////////////////////////////////////////

$sql = "SELECT * FROM tblprojects WHERE project_label IS NOT NULL AND set_active = 1 ORDER BY project_label";

$projects = sqlLoad($sql, $db);

$sql = "SELECT * FROM tblusers WHERE set_active = 1 ORDER BY name_display";

$users = sqlLoad($sql, $db);

$sql = "SELECT * FROM tbltasks WHERE TaskID = ".$_GET['ID'];

$task = sqlLoad($sql, $db);

$sql = "SELECT * FROM tbltask_notes WHERE TaskID = ".$_GET['ID'];

$notes = sqlLoad($sql, $db);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Task <?php echo $task[0]['TaskID'];?> - <?php echo $task[0]['Subject'];?> | SynergyBugs | SynergyMX.com</title>
<script src="https://www.google.com/jsapi?key=<?php echo $api_key;?>" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

    google.load("jquery", "1.4.4");
    google.load("jqueryui", "1.8.6");

</script>
<script language="javascript" type="application/javascript" src="scripts/jquery-forms.js"></script>
<script language="javascript" type="text/javascript">

function loadTLSP(divid, formid) {
    var options = { target: divid, success: loadTLSP };
    $(formid).submit(function() { $(this).ajaxSubmit(options); return false; });

};

<?php if($_GET['up']==1) $message = "Task Updated Successfully!"; ?>
<?php if($_GET['up']==2) $message = "Updated Added Successfully!"; ?>
</script>
<script language="javascript" type="application/javascript" src="scripts/tiny_mce/tiny_mce.js"></script>
<link href="scripts/theme/ui.base.css" rel="stylesheet" type="text/css" />
<link href="scripts/theme/ui.all.css" rel="stylesheet" type="text/css" />
<link href="scripts/theme/ui.theme.css" rel="stylesheet" type="text/css" />
<link href="scripts/theme/ui.datepicker.css" rel="stylesheet" type="text/css" />
<link href="scripts/style.css" rel="stylesheet" type="text/css" />
<link href="scripts/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	$(function() {
		$('#date_due').datepicker({
			showButtonPanel: true
		});
                setTimeout(function() {$("#message").slideUp(200)}, 2000);
	});

        function newUpd(){

		$("#dia_addupd").dialog({
			bgiframe: true,
			width: 550,
			modal: true,
			position: 'top',
			beforeclose: function() {$(this).dialog('destroy');},
			buttons: {
				'Save and Add Update': function() {

					$('#formPNN').ajaxSubmit();

					location.reload();

					return false;

				},
				Cancel: function() {
					$(this).dialog('destroy');
				}
			}
		});

	};

    </script>
    <style>

	.note {padding:5px; border-bottom:1px solid #CCC;}
	.noteby {color:#CCC; font-size:10px;}

	</style>

</head>
<body>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td class="tnbg"><table align="center" width="780" border="0" cellspacing="0" cellpadding="0">
                  <tr><td width="195"><a href="home.php"><img src="/images/logo.png" width="195" height="56" border="0"></a></td><td valign="top">
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td class="tnfck ft8b" height="16" align="right">Welcome Back, <?php echo $_SESSION['name_display'];?>&nbsp;&nbsp;::&nbsp;&nbsp;Sign Out</td>
                    </tr>
                    <tr>
                      <td height="40" align="right"><form id="form1" name="form1" method="post" action="">
                      <a href="home.php"> Task List</a>&nbsp;|&nbsp;
<?php if(sizeof($projects)>0){ ?>
                        <a href="javascript:;" onclick="newUpd();">add update</a>&nbsp;|&nbsp;
                        <?php } ?>
                          <input name="Search" type="text" id="Search" value="Search" />

                          <select name="fbs" id="fbs">
                            <option value="all">All Status</option>
                          </select>

<input name="Refresh" type="submit" id="Refresh" value="Refresh" />

                      </form></td>
                    </tr>
                </table></td></tr></table></td>
              </tr>
              <tr>
                  <td align="center" width="780"><?php if(strlen($message)>0){ ?><div id="message"><?php echo $message;?></div><?php } ?>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="460" valign="top"><form id="formVNT" name="formVNT" method="post" action="task.php?ID=<?php echo $_GET['ID'];?>">
      <table border="0" cellpadding="3" cellspacing="0">
        <tr>
          <td>Subject:</td>
          <td colspan="3"><input name="Subject" type="text" id="Subject" style="width:95%" value="<?php echo $task[0]['Subject'];?>" /></td>
          <td nowrap="nowrap">Finished:</td>
          <td><select name="set_complete" id="set_complete" style="text-align:right">
            <option value="0" <?php if (!(strcmp(0, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>0%</option>
            <option value="10" <?php if (!(strcmp(10, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>10%</option>
            <option value="20" <?php if (!(strcmp(20, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>20%</option>
            <option value="30" <?php if (!(strcmp(30, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>30%</option>
            <option value="40" <?php if (!(strcmp(40, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>40%</option>
            <option value="50" <?php if (!(strcmp(50, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>50%</option>
            <option value="60" <?php if (!(strcmp(60, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>60%</option>
            <option value="70" <?php if (!(strcmp(70, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>70%</option>
            <option value="80" <?php if (!(strcmp(80, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>80%</option>
            <option value="90" <?php if (!(strcmp(90, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>90%</option>
            <option value="100" <?php if (!(strcmp(100, $task[0]['set_complete']))) {echo "selected=\"selected\"";} ?>>100%</option>
          </select></td>
        </tr>
        <tr>
          <td nowrap="nowrap">Date Due:</td>
          <td><input name="date_due" type="text" id="date_due" value="<?php echo date("m/d/Y",strtotime($task[0]['date_due']));?>" size="12" maxlength="12" /></td>
          <td>Status:</td>
          <td><select name="set_status" id="set_status" style="width:100px;">
              <option value="0" <?php if (!(strcmp(0, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Not Started</option>
              <option value="1" <?php if (!(strcmp(1, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Inprocess</option>
              <option value="2" <?php if (!(strcmp(2, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Waiting on others</option>
              <option value="3" <?php if (!(strcmp(3, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Deferred till later</option>
              <option value="4" <?php if (!(strcmp(4, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Pending Review</option>
              <option value="5" <?php if (!(strcmp(5, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Resolved</option>
              <option value="9" <?php if (!(strcmp(9, $task[0]['set_status']))) {echo "selected=\"selected\"";} ?>>Deleted</option>
            </select>          </td>
          <td>Priority:</td>
          <td><select name="set_priority" id="set_priority" style="width:100px;">
              <option value="0" <?php if (!(strcmp(0, $task[0]['set_priority']))) {echo "selected=\"selected\"";} ?>>Low</option>
              <option value="1" <?php if (!(strcmp(1, $task[0]['set_priority']))) {echo "selected=\"selected\"";} ?>>Normal</option>
              <option value="2" <?php if (!(strcmp(2, $task[0]['set_priority']))) {echo "selected=\"selected\"";} ?>>High</option>
              <option value="3" <?php if (!(strcmp(3, $task[0]['set_priority']))) {echo "selected=\"selected\"";} ?>>Critical</option>
          </select></td>
        </tr>
        <tr>
          <td>Project:</td>
          <td><select name="ProjectID" id="ProjectID">
              <option value="" <?php if (!(strcmp("", $task[0]['ProjectID']))) {echo "selected=\"selected\"";} ?>>Select</option>
              <?php foreach($projects as $project){ ?>
              <option value="<?php echo $project['ProjectID'];?>" <?php if (!(strcmp($project['ProjectID'], $task[0]['ProjectID']))) {echo "selected=\"selected\"";} ?>>
              <?php echo $project['project_label'];?>
              </option>
              <?php } ?>
            </select>          </td>
          <td>Task Type: </td>
          <td><select id="set_type" name="set_type">
              <option value="0" <?php if (!(strcmp(0, $task[0]['set_type']))) {echo "selected=\"selected\"";} ?>>Bug</option>
              <option value="1" <?php if (!(strcmp(1, $task[0]['set_type']))) {echo "selected=\"selected\"";} ?>>Idea</option>
              <option value="2" <?php if (!(strcmp(2, $task[0]['set_type']))) {echo "selected=\"selected\"";} ?>>To-Do</option>
              <option value="3" <?php if (!(strcmp(3, $task[0]['set_type']))) {echo "selected=\"selected\"";} ?>>Update</option>
              </select></td>
          <td>Assigned To:</td>
          <td><select name="UserID" id="UserID" style="width:100px;">
              <option value="" <?php if (!(strcmp("", $task[0]['UserID']))) {echo "selected=\"selected\"";} ?>>Select</option>
              <?php foreach ($users as $user){ ?>
              <option value="<?php echo $user['UserID'];?>" <?php if (!(strcmp($user['UserID'], $task[0]['UserID']))) {echo "selected=\"selected\"";} ?>>
              <?php echo $user['name_display'];?>
              </option>
              <?php $user_list[$user['UserID']] = $user['name_display'];

			  } ?>
          </select></td>
        </tr>
        <tr>
          <td colspan="6"><textarea name="Body" id="Body" cols="60" rows="10"><?php echo $task[0]['Body'];?></textarea></td>
        </tr>
		<tr><td colspan="6">Include An Update Note<BR><textarea name="note_body" id="note_body" cols="45" rows="5" style="width:100%;"></textarea></td></tr>
        <tr>
          <td colspan="6" align="center" bgcolor="#F0F0F0"><input type="submit" name="button" id="button" value="Save and Update Task" />
              <input type="hidden" name="TaskID" id="TaskID" value="<?php echo $_GET['ID'];?>" />
            <input type="hidden" name="MM_insert" value="formVNT" /></td>
        </tr>
      </table>
    </form></td>
    <td width="320" valign="top" style="padding-left:10px;"><div id="notes"> Updates to this task
          <?php if(sizeof($notes)==0){ ?>
          <p>No Notes Found</p>
      <?php } else {

foreach ($notes as $note){

?>
      <div class="note"><?php echo $note['Body'];?><div class="noteby"><?php echo date("M jS y h:i A",strtotime($note['date_created']));?> by <?php echo $user_list[$note['UserID']];?></div></div>
  <?php }
} ?>

    </div></td>
  </tr>
</table>
</td>
</tr>
</table>
 <div style="display:none" id="dia_addupd" name="dia_addupd" title="Add Update">
  <form id="formPNN" name="formPNN" method="post" action="/task.php">
    <textarea name="note_body" id="note_body" cols="45" rows="5" style="width:100%"></textarea>
    <input type="hidden" name="TaskID" id="TaskID" value="<?php echo $_GET['ID'];?>" />
    <input type="hidden" name="UserID" id="UserID" value="<?php echo $_SESSION['UserID'];?>" />
    <input type="hidden" name="add_note" id="add_note" value="add_note" />
  </form>
  </div>
<script language="javascript">
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : "Body",
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "silver",
		plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",

		// Theme options
		theme_advanced_buttons1 : "print,fullscreen,preview,fontsizeselect,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,backcolor,forecolor,",
		theme_advanced_buttons2 : "cut,copy,pasteword,|,search,replace,|,cleanup,removeformat,|,tablecontrols,|,link,unlink,emotions,charmap",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		height  : "480px",
		width  : "545px"
	});



</script>
</body>
</html>